@echo off
chcp 65001

set PROJECT_NAME=enterprise-dev-template
set VERSION=1.0.0
set DOCKER_IMAGE=%PROJECT_NAME%:%VERSION%

echo ========================================
echo 1. 构建 Nuxt 前端
cd nuxt
call pnpm run build
if errorlevel 1 (
    echo Nuxt构建失败 & pause & exit /b 1
)
cd ..

echo ========================================
echo 2. 构建 Vue 前端
cd web
call pnpm install || echo pnpm install 有 warning，继续执行
call pnpm run build
if errorlevel 1 (
    echo Vue构建失败 & pause & exit /b 1
)
cd ..

echo ========================================
echo 3. 构建 Go 后端
cd server
set GOOS=linux
set GOARCH=amd64
set CGO_ENABLED=0
call go mod tidy
call go build -o app-linux -ldflags="-s -w" .
if errorlevel 1 (
    echo Go构建失败 & pause & exit /b 1
)
cd ..

echo ========================================
echo 4. 移动构建产物到 deploy 目录
if exist deploy\nuxt rmdir /s /q deploy\nuxt
if exist deploy\dist rmdir /s /q deploy\dist
if exist deploy\server rmdir /s /q deploy\server

xcopy /E /I /Y nuxt\.output deploy\nuxt\
xcopy /E /I /Y web\dist deploy\dist\
xcopy /E /I /Y server\app-linux deploy\server\
xcopy /E /I /Y server\config.yaml deploy\server\

echo ========================================
echo 5. 构建 Docker 镜像
cd deploy
docker build -f Dockerfile -t %DOCKER_IMAGE% .
if errorlevel 1 (
    echo Docker镜像构建失败 & pause & exit /b 1
)
cd ..

echo ========================================
echo 构建完成！镜像名：%DOCKER_IMAGE%
pause
